﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class MemberNoteTreeDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion		

        #region Methods

        /// <summary>
        /// Saves a record to the Member_NoteTree table.
        /// </summary>
        public virtual int Insert(MemberNoteTreeInfo memberNoteTreeInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{				
				new SqlParameter("@CreatedOn", memberNoteTreeInfo.CreatedOn),
				new SqlParameter("@IdMember", memberNoteTreeInfo.IdMember),
				new SqlParameter("@FullName", memberNoteTreeInfo.FullName),
				new SqlParameter("@Status", memberNoteTreeInfo.Status),
                new SqlParameter("@IdMember_Active", memberNoteTreeInfo.IdMember_Active),
                new SqlParameter("@Position", memberNoteTreeInfo.Position),
                new SqlParameter("@CodeMemberParent", memberNoteTreeInfo.CodeMemberParent),
                new SqlParameter("@FullName_Active", memberNoteTreeInfo.FullName_Active)
                
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Member_NoteTree_Insert", parameters);
        }

        /// <summary>
        /// Updates a record in the Member_NoteTree table.
        /// </summary>
        public virtual void Update(int id, int status, int idMember_Active, int checkStatus, string position, int check)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id",id),
				new SqlParameter("@Status", status),
				new SqlParameter("@IdMember_Active", idMember_Active),
				new SqlParameter("@CheckStatus", checkStatus),	
                new SqlParameter("@Position", position),			        
                new SqlParameter("@Check",check)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Member_NoteTree_Update", parameters);
        }

        /// <summary>
        /// Deletes a record from the Member_NoteTree table by its primary key.
        /// </summary>
        public virtual void Delete(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Member_NoteTree_Delete", parameters);
        }

        /// <summary>
        /// Selects a single record from the Member_NoteTree table.
        /// </summary>
        public virtual MemberNoteTreeInfo Select(int status, string param, string param1)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Status", status),
                new SqlParameter("@Param", param),
                new SqlParameter("@Param1", param1)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Member_NoteTree_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeMemberNoteTreeInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

        public virtual DataTable Select_Notte(int status, string param, string param1)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Status", status),
                new SqlParameter("@Param", param),
                new SqlParameter("@Param1", param1)
			};

            DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure,
                                                             "Member_NoteTree_Select", parameters);
            {
                if (dt !=null && dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Selects all records from the Member_NoteTree table.
        /// </summary>
        public CHRTList<MemberNoteTreeInfo> ListSelect(int status, string param,string param1)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Status", status),
                new SqlParameter("@Param", param),
                new SqlParameter("@Param1", param1)
			};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Member_NoteTree_Select", parameters))
            {
                CHRTList<MemberNoteTreeInfo> memberNoteTreeInfoList = new CHRTList<MemberNoteTreeInfo>();
                while (dataReader.Read())
                {
                    MemberNoteTreeInfo memberNoteTreeInfo = MakeMemberNoteTreeInfo(dataReader);
                    memberNoteTreeInfoList.Add(memberNoteTreeInfo);
                }

                return memberNoteTreeInfoList;
            }
        }
        /// <summary>
        /// Tính tổng số nút
        /// </summary>
        /// <param name="Idmember"></param>
        /// <returns></returns>
        public int CountNoteTree(string Idmember)
        {
            int sumTree=0;
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@Idmember", Idmember)                                              
                                            };            
                sumTree=(int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Member_NoteTree_SumNoteTree", parameters);
                if (sumTree > 0)
                    return sumTree;
            return 0;

        }

        public int CountStatus()
        {
            int _count = 0;
            _count = (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.Text, "select COUNT(*) from Member_NoteTree where Status > 0");
            if (_count > 0)
                return _count;
            return 0;

        }

        /// <summary>
        /// Creates a new instance of the Member_NoteTree class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual MemberNoteTreeInfo MakeMemberNoteTreeInfo(SqlDataReader dataReader)
        {
            MemberNoteTreeInfo memberNoteTreeInfo = new MemberNoteTreeInfo();
            memberNoteTreeInfo.Id = SqlClientUtility.GetInt32(dataReader, DbConstants.MEMBER_NOTETREE.ID, 0);
            memberNoteTreeInfo.CreatedOn = SqlClientUtility.GetDateTime(dataReader, DbConstants.MEMBER_NOTETREE.CREATEDON, DateTime.Now);
            memberNoteTreeInfo.IdMember = SqlClientUtility.GetInt32(dataReader, DbConstants.MEMBER_NOTETREE.IDMEMBER, 0);
            memberNoteTreeInfo.FullName = SqlClientUtility.GetString(dataReader, DbConstants.MEMBER_NOTETREE.FULLNAME, String.Empty);
            memberNoteTreeInfo.Status = SqlClientUtility.GetInt32(dataReader, DbConstants.MEMBER_NOTETREE.STATUS, 0);
            memberNoteTreeInfo.IdMember_Active = SqlClientUtility.GetInt32(dataReader, DbConstants.MEMBER_NOTETREE.IDMEMBER_ACTIVE, 0);
            memberNoteTreeInfo.Position = SqlClientUtility.GetString(dataReader, DbConstants.MEMBER_NOTETREE.POSITION, String.Empty);
            memberNoteTreeInfo.CodeMemberParent = SqlClientUtility.GetString(dataReader, DbConstants.MEMBER_NOTETREE.CODEMEMBERPARENT, String.Empty);
            memberNoteTreeInfo.FullName_Active = SqlClientUtility.GetString(dataReader, DbConstants.MEMBER_NOTETREE.FULLNAME_ACTIVE, String.Empty);                        
            return memberNoteTreeInfo;
        }

        #endregion
	}
}
